﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HZ.Data;
using System.Data;
using System.Data.SqlClient;
using HZ.BidSystem.Model;

namespace HZ.BidSystem.DAL
{
    /// <summary>
    /// 用户账户数据层操作
    /// </summary>
    public class DAL_Account
    {
        public static int Login(string username, string password, out HZ.BidSystem.Model.Account account)
        {
            string sql = "SELECT * FROM TB_Account WHERE AccountName = @AccountName";
            IDbHelper db = DbHelperFactory.Create();
            IDbDataParameter para = new SqlParameter("@AccountName", username);
            try
            {
                DataTable dt = db.ExecuteTable(CommandType.Text, sql, para);
                if (dt.Rows.Count > 0)
                {
                    account = DAL_Helper.CommonFill<Account>(dt.Rows[0]);
                    if (!(account.IsUsing ?? false))
                    {
                        return 5;
                    }

                    if (string.Compare(account.Password, HZ.Fun.GetMd5(password), true) == 0)
                    {
                        account.LastLogin = DateTime.Now;
                        return 1;
                    }
                    else
                    {
                        account = null;
                        return 3;
                    }

                }
                else
                {
                    account = null;
                    return 2;
                }
            }
            catch (Exception ee)
            {
                HZ.Common.log4netManager.LogDebug("DAL_Account", ee.Message, ee);
#if DEBUG
                throw;
#endif

                account = null;
                return 4;

            }


        }

        public static int ChangePassword(int AccountId, string oldpassword, string newpassword)
        {
            var account = HZ.Data.DAL_Helper.GetModelByID<Account>(new Account() { AccountId = AccountId });

            if (account == null)
            {
                return 3;
            }
            else
            {
                if (string.Compare(HZ.Fun.GetMd5(oldpassword), account.Password, true) != 0)
                {
                    return 2;
                }
                else
                {
                    var acc = new Account() { AccountId = AccountId, Password = HZ.Fun.GetMd5(newpassword) };

                    return HZ.Data.DAL_Helper.OperateTableSQL(acc, TableOperator.Update);
                }


            }
        }

        public static int Login(string username, out Account account)
        {
            string sql = "SELECT * FROM TB_Account WHERE AccountName = @AccountName";
            IDbHelper db = DbHelperFactory.Create();
            IDbDataParameter para = new SqlParameter("@AccountName", username);
            try
            {
                DataTable dt = db.ExecuteTable(CommandType.Text, sql, para);
                if (dt.Rows.Count > 0)
                {
                    account = DAL_Helper.CommonFill<Account>(dt.Rows[0]);

                    return 1;
                }
                else
                {
                    account = null;
                    return 2;
                }
            }
            catch (Exception ee)
            {
                HZ.Common.log4netManager.LogDebug("DAL_Account", ee.Message, ee);
#if DEBUG
                throw;
#endif

                account = null;
                return 3;

            }
        }
    }
}
